package com.zzj.ecology.system.dto;//package com.zzj.ecology.system.dto;

/**
 * @ClassName HttpResult
 * @Description TODO
 * @Author zhijun zeng at 23:13 2019/4/17
 * @Version 1.0
 **/

import com.zzj.ecology.common.enums.LoginResultEnum;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class HttpResult implements Serializable {

    public static final String SUCCESS_CODE = "100";
    public static final String FAILURE_CODE = "200";

    public static final String SUCCESS_TAG = "SUCCESS";
    public static final String FAILURE_TAG  = "ERROR";

    public static final String DATA_TAG = "data";
    public static final String PAGE_TAG = "page";
    public static final String LIST_TAG = "list";
    public static final String PAR_LIST_TAG = "parlist";//主LIST
    public static final String SUB_LIST_TAG = "sublist";//辅LIST

    public enum ErrorType{
        REPEAT_SUBMIT("10000","请不要重复提交");

        private String code;
        private String name;
        public String getCode() {
            return code;
        }
        public String getName() {
            return name;
        }
        private ErrorType(String code, String name) {
            this.code = code;
            this.name = name;
        }
    }



    private String code;
    private String name;
    private Map<String, Object> extend = new HashMap<String, Object>();
    private Object data;

    public HttpResult() {}
    public HttpResult(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public HttpResult(LoginResultEnum loginResult) {//通过枚举来获取信息
        this.code = loginResult.getCode();
        this.name = loginResult.getName();
    }

    public HttpResult(ErrorType errorType) {//通过自身枚举获取
        this.code = errorType.code;
        this.name = errorType.name;
    }

    public static HttpResult success() {
        HttpResult hr = new HttpResult(SUCCESS_CODE, "处理成功");
        return hr;
    }

    public static HttpResult error() {
        HttpResult hr = new HttpResult(FAILURE_CODE, "处理失败");
        return hr;
    }
    public static HttpResult error(String errMsg) {
        HttpResult hr = new HttpResult(FAILURE_CODE, errMsg);
        return hr;
    }

    public HttpResult add(Object object) {
        return add(HttpResult.DATA_TAG, object);
    }

    public static HttpResult addData(HttpResult result, Object object) {
        result.setData(object);
        return result;
    }

    public HttpResult add(String key, Object value) {
        this.getExtend().put(key, value);
        return this;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Map<String, Object> getExtend() {
        return extend;
    }

    public void setExtend(Map<String, Object> extend) {
        this.extend = extend;
    }

}